package com.bwie.service.impl;

import cn.hutool.db.Page;
import com.bwie.exception.BusinessException;
import com.bwie.mapper.MonthMapper;
import com.bwie.pojo.dto.MonthDto;
import com.bwie.pojo.po.CarMonth;
import com.bwie.pojo.po.Month;
import com.bwie.pojo.po.User;
import com.bwie.service.MonthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Service
public class MonthServiceImpl implements MonthService {
    @Autowired
    private MonthMapper monthMapper;
    @Autowired
    private HttpSession session;

    @Override
    public PageInfo<Month> getMonthList(MonthDto monthDto) {
        PageHelper.startPage(monthDto.getPageNum(),monthDto.getPageSize());
        List<Month> list = monthMapper.getMonthList(monthDto);
        PageInfo<Month> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Month> getMonthById(Integer parkId) {
        List<Month> list = monthMapper.getMonthById(parkId);


        return list;
    }

    @Override
    public Month selectMonthById(Integer monthId) {


        return monthMapper.selectMonthById(monthId);
    }

    /**
     * 办卡
     * @param carMonth
     */
    @Override
    public void addCarMonth(CarMonth carMonth) {
        /**
         * 非空校验
         */
        Integer monthId = carMonth.getMonthId();
        String monthType = carMonth.getMonthType();
        String carCard = carMonth.getCarCard();
        BigDecimal monthPrice = carMonth.getMonthPrice();
        Integer useTimes = carMonth.getUseTimes();
        if (monthId==null|| StringUtils.isEmpty(monthType)||StringUtils.isEmpty(carCard)||monthPrice==null||useTimes==null){
            throw new BusinessException("必要字段不能为空！！！");
        }
        /**
         * 判断是否办过该月卡
         */
        CarMonth carMonth1= monthMapper.selectMonth(carMonth);
        if (null!=carMonth1){
            throw new BusinessException("不能重复办理月卡");
        }

        User user = (User) session.getAttribute("user");
        carMonth.setUserId(user.getUserId());
        monthMapper.addCarMapper(carMonth);


    }
}
